package cn.lyh.gulimall.product.controller;

import cn.lyh.gulimall.product.entity.PmsAttr;
import cn.lyh.gulimall.product.entity.PmsAttrAttrgroupRelation;
import cn.lyh.gulimall.product.entity.PmsAttrGroup;
import cn.lyh.gulimall.product.service.PmsAttrAttrgroupRelationService;
import cn.lyh.gulimall.product.service.PmsAttrGroupService;
import cn.lyh.gulimall.product.service.PmsAttrService;
import cn.lyh.gulimall.product.service.PmsCategoryService;
import cn.lyh.gulimall.product.vo.AttrRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.PageUtils;
import utils.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author lyh
 * @version 1.0
 * @since 2021/4/12
 */
@RestController
@RequestMapping("/product/attrgroup")
public class PmsAttrGroupController {

	@Autowired
	PmsAttrGroupService attrGroupService;

	@Autowired
	PmsCategoryService categoryService;

	@Autowired
	PmsAttrService attrService;

	@Autowired
	PmsAttrAttrgroupRelationService relationService;

	/**
	 * 根据Id或者前端参数来查询分页查找信息
	 * @param params 前端数据实体
	 * @param catelogId id
	 * @return 操作结果和数据
	 */
	@GetMapping("/list/{catelogId}")
	public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
		PageUtils pageUtils = attrGroupService.queryPage(params, catelogId);
		return R.ok().put("page", pageUtils);

	}

	/**
	 * 根据Id查询详情
	 * @param attGroupId id
	 * @return 操作结果和数据
	 */
	@GetMapping("/info/{attGroupId}")
	public R info(@PathVariable("attGroupId") Long attGroupId) {
		PmsAttrGroup attrGroup = attrGroupService.getById(attGroupId);
		// 获取完整的catlogPath
		Long catelogId = attrGroup.getCatelogId();
		Long[] path = categoryService.findCatelogPath(catelogId);
		attrGroup.setCatelogPath(path);
		return R.ok().put("attrGroup", attrGroup);
	}

	/**
	 * 新增方法
	 * @param pmsAttrGroup 数据实体
	 * @return 操作结果
	 */
	@PostMapping("/save")
	public R save(@RequestBody PmsAttrGroup pmsAttrGroup) {
		attrGroupService.save(pmsAttrGroup);
		return R.ok();
	}

	/**
	 * 数据更新方法
	 * @param pmsAttrGroup 数据实体，根据Id去修改数据
	 * @return 操作结果
	 */
	@PostMapping("/update")
	public R update(@RequestBody PmsAttrGroup pmsAttrGroup) {
		attrGroupService.updateById(pmsAttrGroup);
		return R.ok();
	}

	/**
	 * 删除方法
	 * @param ids 根据id数组来进行批量删除
	 * @return 操作结果
	 */
	@PostMapping("/delete")
	public R delete(@RequestBody Long[] ids){
		attrGroupService.removeByIds(Arrays.asList(ids));
		return R.ok();
	}

	@GetMapping("/{attrgroupId}/attr/relation")
	public R getAttrRelation(@PathVariable("attrgroupId")Long attrgroupId){
		List<PmsAttr> attrRelation = attrGroupService.getAttrRelation(attrgroupId);
		return R.ok().put("data",attrRelation);
	}

	///product/attrgroup/attr/relation/delete
	@PostMapping("/attr/relation/delete")
	public R delete(@RequestBody AttrRelationVo[] attrRelationVos){
		attrService.deleteRelations(attrRelationVos);
		return R.ok();
	}


	// /product/attrgroup/{attrgroupId}/noattr/relation
	@GetMapping("/{attrgroupId}/noattr/relation")
	public R getAttrNoRelation(@PathVariable Long attrgroupId,
							   @RequestParam Map<String,Object> params){
		PageUtils pageUtils = attrGroupService.getAttrNoRelation(attrgroupId,params);
		return R.ok().put("page",pageUtils);
	}

	@PostMapping("/attr/relation")
	public R saveAttrRelation(@RequestBody List<AttrRelationVo> vos){
		relationService.saveBatch(vos);
		return R.ok();
	}
}
